package com.onetech.adrewards.free.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.dialogs.FeedbackDialog;
import com.onetech.adrewards.free.dialogs.GuideDialogFragment;
import com.onetech.adrewards.free.dialogs.LoginBonusDialog;

import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.models.UserModel;
import com.onetech.adrewards.free.objects.UserInfoDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;

public class BaseActivity extends FragmentActivity implements
		OnClickListener, OnPageChangeListener{
	private LinearLayout mViewContainer = null;
	
	private View menuBottomLeft = null;
	private View menuBottomMiddle = null;
	private View menuBottomRight = null;
	//private QuickAction mFirstMenu = null;
	private TextView mAppTitle=null;	
	private ImageView mHomeBanner = null;
	private ProgressBar mCoinProgressBar = null;
	private TextView mMyCoins = null;
	private TextView mMaxCoins = null;
	private View mQuestionBtn=null;
	
	private View mFooterView=null;
	private View mMyPointContainer=null;
	private View mContentProgressbarCoins=null;
	
	private final int MIN_COIN = 0;
	protected int mUserCoins=0;
	protected int currentView = R.id.menuMiddle;

	
	protected GetUserInfo mGetUserInfo = null;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.base_activity_layout);
		initView();
		initListener();	
		overridePendingTransition(R.anim.zuzu_fade_in, R.anim.zuzu_fade_out);
	}
	@Override
	public void finish() {
		super.finish();
		
	}
	private void initView() {
		mViewContainer = (LinearLayout) findViewById(R.id.mViewContainer);
		mFooterView=(LinearLayout)findViewById(R.id.appFooter);
		
		menuBottomLeft = (LinearLayout) findViewById(R.id.menuLeft);
		menuBottomMiddle = (LinearLayout) findViewById(R.id.menuMiddle);
		menuBottomRight = (LinearLayout) findViewById(R.id.menuRight);
		mHomeBanner = (ImageView) findViewById(R.id.homeBanner);
		mCoinProgressBar = (ProgressBar) findViewById(R.id.myProgressBarPoint);
		mCoinProgressBar.setMax(AppConfig.MIN_COIN_EXCHANGE);
		mMyPointContainer=(LinearLayout)findViewById(R.id.myPoints);
		mContentProgressbarCoins=(LinearLayout)findViewById(R.id.myContentProgressBarCoins);
		
		mAppTitle=(TextView)findViewById(R.id.appTitle);
		mMyCoins = (TextView) findViewById(R.id.myCoins);
		mMaxCoins = (TextView) findViewById(R.id.maxCoins);
		mQuestionBtn=(LinearLayout)findViewById(R.id.appQuestion);
		setDefaultMenuBackground();
		
		/*mFirstMenu = new QuickAction(this);
		mFirstMenu.setOnActionItemClickListener(this);
		setQuickAction(mFirstMenu, R.drawable.ic_launcher);*/
	}
	protected void setAppTittle(String title){
		mAppTitle.setText(title);
	}
	private void initListener() {
		mHomeBanner.setOnClickListener(this);
		mContentProgressbarCoins.setOnClickListener(this);
		menuBottomLeft.setOnClickListener(this);
		menuBottomMiddle.setOnClickListener(this);
		menuBottomRight.setOnClickListener(this);
		mQuestionBtn.setOnClickListener(this);

	}
	protected void showFooterView(boolean isShow){
		mFooterView.setVisibility(isShow?View.VISIBLE:View.GONE);
	}
	protected void showMyPoints(boolean isShow){
		mMyPointContainer.setVisibility(isShow?View.VISIBLE:View.GONE);
	}
	protected void setMainView(int resId) {
		LayoutInflater inflater = LayoutInflater.from(this);
		inflater.inflate(resId, mViewContainer);
	}
	
	protected void updateCoins(String myCoins, String maxCoins) {
		try {
			mMyCoins.setText(myCoins);
			mMaxCoins.setText(maxCoins);
			mCoinProgressBar.setProgress(Integer.valueOf(myCoins));
		} catch (Exception e) {
			return;
		}
	}
	public void showFeedbackDialog(){
		FeedbackDialog mFeedbackDialog = new FeedbackDialog(this);
		mFeedbackDialog.show();		
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.appQuestion:			
			new GuideDialogFragment().show(getSupportFragmentManager(), "");
			//Intent mSocialShareIntent=new Intent(getApplicationContext(), SocialShareActivity.class);
			//startActivity(mSocialShareIntent);
			//Intent mExchangeActivity = new Intent(getApplicationContext(), ExchangeActivity.class);
			//startActivity(mExchangeActivity);
			
			break;
		case R.id.homeBanner:
			LoginBonusDialog mLoginBonusDialog = new LoginBonusDialog(this);
			mLoginBonusDialog.show();						
			break;
		case R.id.myContentProgressBarCoins:
			Intent intent = new Intent(getApplicationContext(), PointHistoryActivity.class);
			startActivity(intent);
			return;
		case R.id.menuLeft:
			currentView = R.id.menuLeft;
			break;
		case R.id.menuMiddle:
			currentView = R.id.menuMiddle;
			break;
		case R.id.menuRight:
			currentView = R.id.menuRight;
			break;
		default:
			break;
		}
		switchLayout(currentView);
	}

	private void setDefaultMenuBackground() {
		menuBottomLeft
				.setBackgroundResource(R.drawable.menu_bottom_background_selected);
		menuBottomMiddle
				.setBackgroundResource(R.drawable.menu_bottom_background_normal);
		menuBottomRight
				.setBackgroundResource(R.drawable.menu_bottom_background_normal);
	}

	protected void switchLayout(int id) {
		switch (id) {
		case R.id.menuLeft:
			menuBottomLeft
					.setBackgroundResource(R.drawable.menu_bottom_background_selected);
			menuBottomMiddle
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomRight
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			break;
		case R.id.menuMiddle:
			menuBottomLeft
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomMiddle
					.setBackgroundResource(R.drawable.menu_bottom_background_selected);
			menuBottomRight
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			break;
		case R.id.menuRight:
			menuBottomLeft
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomMiddle
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomRight
					.setBackgroundResource(R.drawable.menu_bottom_background_selected);
			break;
		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {

	}

	@Override
	protected void onResume() {
		super.onResume();
		getUIDFromStore();
		getUserCoinsFromStore();
	}
	public int getUserCoins(){
		return mUserCoins;
	}
	protected void getUIDFromStore(){
		AdrewadsUtils.checkUId();
	}
	protected void getUserCoinsFromStore(){
		try {
			String coins=CommonModel.getInstance().getStore(Cache.ONE_TECH_USER_POINTS);
			if(coins!=null){
				mUserCoins=Integer.valueOf(coins);
			}			
		} catch (Exception e) {			
		}	
	}
	protected void getUserInfo() {
		if (mGetUserInfo == null || mGetUserInfo.getStatus() == Status.FINISHED) {
			mGetUserInfo = new GetUserInfo();
			mGetUserInfo.execute();
		}
	}

	private class GetUserInfo extends AsyncTask<Void, Void, UserInfoDTO> {
		@Override
		protected UserInfoDTO doInBackground(Void... params) {
			return UserModel.getInstance().getUserInfo(AppConfig.uId);
		}

		@Override
		protected void onPostExecute(UserInfoDTO result) {
			super.onPostExecute(result);

			try {
				if (result != null && result.getTotal_coin().trim().length()>0) {
					//AppConfig.userCoins=Integer.valueOf(result.getTotal_coin().trim());
					mUserCoins=Integer.valueOf(result.getTotal_coin().trim());
					
					updateCoins(result.getTotal_coin(),
							String.valueOf(AppConfig.MIN_COIN_EXCHANGE));
					// mCoinProgressBar.setProgress(Integer.valueOf(result.getTotal_coin()));
				}
			} catch (Exception e) {
				return;
			}
		}
	}
	@Override
	public void onBackPressed() {
		super.onBackPressed();		
	}
	
	public void go2HomePage(){
		try {
			Intent intent=new Intent(getApplicationContext(), HomeActivity.class);
			intent.putExtra("m_id", "2835");
			intent.putExtra("m_owner_id", "558");
			if( AppConfig.uId!=null && AppConfig.uId.trim().length()>0){
				
				intent.putExtra("user_id", AppConfig.uId.trim());
				intent.putExtra("uid", AppConfig.uId.trim());
			}else{
				intent.putExtra("user_id", "383cb3f413cc96b69cc78b818e433748");//account test
				intent.putExtra("uid", "383cb3f413cc96b69cc78b818e433748");//account test
			}
			intent.putExtra("platform_id", "0");
			intent.putExtra("url", "http://car.mobadme.jp/spg/spnew/558/2835/index.php");
			intent.putExtra("api_key", "76552d4faa4dd35b");
			intent.putExtra("app_key", "ncIdX3la");
			intent.putExtra("loading_msg", "loading..");
			intent.putExtra("timeout", "3000");

			////
			
			intent.putExtra("appkey", "ncIdX3la");
			////

			intent.putExtra("show_button", true);
			intent.putExtra("button_string", "back");
			intent.putExtra("bk_color", Color.BLUE);
			intent.putExtra("button_gravity", Gravity.RIGHT);
			
			startActivity(intent);
			finish();
		} catch (Exception e) {
			return;
		}
	}
	
}
